package demo;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: user
 * Date: 2023-06-18
 * Time: 15:06
 */
public class MyStack {

    private Queue<Integer> que1 = new LinkedList<>();
    private Queue<Integer> que2 = new LinkedList<>();

    public void push(int x){
        if (!que1.isEmpty()){
            que1.offer(x);
        }else if (!que2.isEmpty()){
            que2.offer(x);
        }else {
            que1.offer(x);
        }
    }
    public int pop(){
        if (empty()){
            return -1;
        }
        if (!que1.isEmpty()){
            for (int i = 0; i <que1.size()-1 ; i++) {
                int x = que1.poll();
                que2.offer(x);
            }
            return que1.poll();
        }
        if (!que2.isEmpty()) {
            for (int i = 0; i < que2.size() - 1; i++) {
                int x = que2.poll();
                que1.offer(x);
            }
            return que2.poll();
        }
        return -1;
    }
    public int top(){

        if (empty()){
            return -1;
        }
        if (!que1.isEmpty()){
            int x = -1;
            for (int i = 0; i <que1.size()-1 ; i++) {
                 x = que1.poll();
                que2.offer(x);
            }
            return x;
        }
        if (!que2.isEmpty()) {
            int x = -1;
            for (int i = 0; i < que2.size() - 1; i++) {
                 x = que2.poll();
                que1.offer(x);
            }
            return x;
        }
        return -1;
    }


    public boolean empty(){
        return que1.isEmpty() &&que2.isEmpty();
    }

    public static void main(String[] args) {

    }
}
